﻿using System;
using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.Commands;
using KM.Framework.Utility;

namespace KM.Framework.Business
{
    public abstract partial class ObjectSetViewModel<T> : ViewModelBase
    {
        public event Action<T, T> BeforeSetCurrentObject;
        public event Action AfterSetCurrentObject;

        private string _ResultInfo;
        /// <summary>
        /// 查询到的结果描述文字
        /// </summary>
        public string ResultInfo
        {
            get
            {
                return _ResultInfo;
            }
            set
            {
                _ResultInfo = value;
            }
        }

        public DelegateCommand<T> CurrentObjectSelectChanged;

        private ObservableCollection<T> _CurrentObjectList;
        /// <summary>
        /// 当前处理的对象列表
        /// </summary>
        public ObservableCollection<T> CurrentObjectList
        {
            get
            {
                return _CurrentObjectList ?? (_CurrentObjectList = new ObservableCollection<T>());
            }
            set
            {
                _CurrentObjectList = value;
                this.RaisePropertyChanged(p => p.CurrentObjectList);
            }
        }

        private T _CurrentObject;
        /// <summary>
        /// 当前选中的对象
        /// </summary>
        public virtual T CurrentObject
        {
            get
            {
                return _CurrentObject;
            }
            set
            {
                if (!IsEquals(_CurrentObject, value))
                {
                    if (this.BeforeSetCurrentObject != null)
                    {
                        this.BeforeSetCurrentObject(_CurrentObject, value);
                    }
                    _CurrentObject = value;
                    this.RaisePropertyChanged(p => p.CurrentObject);
                    if (CurrentObjectSelectChanged != null)
                    {
                        CurrentObjectSelectChanged.Execute(value);
                    }

                    if (this.AfterSetCurrentObject != null)
                    {
                        this.AfterSetCurrentObject();
                    }

                    RaiseCommandCanExecuteChanged();
                }
            }
        }

        protected virtual void RaiseCommandCanExecuteChanged()
        {

        }

        protected bool IsEquals(object obj1, object obj2)
        {
            return obj1 == obj2;
        }

        //关键字查询
        private string _KeyWords;
        public virtual string KeyWords
        {
            get { return _KeyWords; }
            set
            {
                _KeyWords = value;
                this.RaisePropertyChanged("KeyWords");
            }
        }
        //滚动对象
        private T _ScrollItem;
        public T ScrollItem
        {
            get { return _ScrollItem; }
            set
            {
                _ScrollItem = value;
                this.RaisePropertyChanged("ScrollItem");
            }
        }
        /// <summary>
        /// 滚动方法(默认滚动到当前对象)
        /// </summary>
        public void ScrollInto()
        {
            ScrollItem = CurrentObject;
            ScrollItem = default(T);
        }
        /// <summary>
        ///  滚动方法
        /// </summary>
        /// <param name="item"></param>
        public void ScrollInto(T item)
        {
            if (item == null)
            {
                ScrollInto();
            }
            else
            {
                ScrollItem = item;
                ScrollItem = default(T);
            }
        }
    }
}
